﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Hi_Baidu.Packet;

namespace Hi_Baidu.Plugin
{
    /// <summary>
    ///     提供了基础的视频协议解析器
    /// </summary>
    class FileShareProtocolParser : IParser
    {
        #region IParser 成员

        public bool Accpet(Dictionary<string, byte[]> pck)
        {
            return PackageHelper.GetProtocol(pck) == Hi_Baidu.Packet.Protocol.NetworkProtocol.TRANSFER_FILE;
        }

        public int GetLength(byte[] pckBody)
        {
            return pckBody.Length;
        }

        public bool NeedAck(Dictionary<string, byte[]> pck)
        {
            bool Result = Encoding.Default.GetString(pck["ACK"]) == "0" ? false : true;
            return Result;
        }

        public InPackage ParserInPackage(Dictionary<string, byte[]> PckContent)
        {
            SendFileInPackage SIP = new SendFileInPackage();
            //ACK Property
            if (Encoding.Default.GetString(PckContent["ACK"]) == "0")
            {
                SIP.IS_ACK = false;
            }
            else
            {
                SIP.IS_ACK = true;
            }
            //Encrypt Property
            if (Encoding.Default.GetString(PckContent["ENCRYPT"]) == "0")
            {
                SIP.IS_ENCRYPT = false;
            }
            else
            {
                SIP.IS_ENCRYPT = true;
            }

            SIP.Protocol = PackageHelper.GetProtocol(PckContent);
            SIP.SendTime = DateTime.Now;
            SIP.Header = Encoding.Default.GetString(PckContent["TYPE"]);
            SIP.Body = PckContent;
            SIP.Command = (Hi_Baidu.Packet.Protocol.IN_SendFile)PackageHelper.GetNetworkCommand(PckContent);
            return SIP;
        }

        #endregion
    }
}
